<template>
  <a-modal
    :visible="visible"
    title="重置密码"
    centered
    @cancel="visible = false"
    @ok="handleSubmit"
  >
    <custom-form
      ref="custom-form"
      class="custom-form"
      v-model="currentRow"
      :FormList="formColumns"
      layout="horizontal"
    ></custom-form>
  </a-modal>
</template>

<script>
import { userServices } from '@/common/services/user'
import CustomForm from '@/components/src/Form'
export default {
  name: 'Register',
  components: {
    CustomForm
  },
  mixins: [],
  data () {
    return {
      visible: false,
      currentRow: {
        usr_psw_old: '',
        usr_psw_new: ''
      },
      formColumns: [
        {
          title: '旧密码',
          dataIndex: 'usr_psw_old',
          key: 'usr_psw_old',
          form: {
            type: 'a-input-password',
            placeholder: '请输入旧密码',
          }
        },
        {
          title: '新密码',
          dataIndex: 'usr_psw_new',
          key: 'usr_psw_new',
          form: {
            type: 'a-input-password',
            placeholder: '请输入新密码',
          }
        },
      ]
    }
  },
  computed: {

  },
  methods: {
    handlePasswordCheck (rule, value, callback) {
      const password = this.form.getFieldValue('password')
      console.log('value', value, password)
      if (value === undefined) {
        callback(new Error('请输入密码'))
      }
      if (value && password && value.trim() !== password.trim()) {
        callback(new Error('两次密码不一致'))
      }
      if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/.test(value)) {
        callback(new Error('密码必须包含字母+数字或者不足8位'))
      }
      callback()
    },

    handlePasswordInputClick () {
      if (!this.isMobile()) {
        this.state.passwordLevelChecked = true
        return
      }
      this.state.passwordLevelChecked = false
    },

    show () {
      this.visible = true
    },

    handleSubmit () {
      console.log(this.currentRow)
      if (!this.currentRow.usr_psw_old) {
        this.$message.error('请输入旧密码')
        return
      }
      if (!this.currentRow.usr_psw_new) {
        this.$message.error('请输入新密码')
        return
      }
      if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/.test(this.currentRow.usr_psw_new)) {
        this.$message.error('密码必须包含字母+数字或者不足8位')
        return
      }
      userServices.resetPassword({
        usr_psw_old: this.currentRow.usr_psw_old,
        usr_psw_new: this.currentRow.usr_psw_new
      }).then(() => {
        this.visible = false
        this.$message.success('密码修改成功')
        this.$emit('success')
      })
    },
  },
  watch: {
  }
}
</script>
<style lang="less" scoped>
</style>
